#include"Acceptor.hh"
Acceptor::Acceptor(const char *ip, int port)
    :_socket()
     ,_addr(ip,port)
{}

Acceptor::~Acceptor()
{}

    void Acceptor::ready()
{
    setreuseip();
    setreuseport();
    bind();
    listen();
}
    void Acceptor::setreuseip()
{
    int n=0;
    int ret=setsockopt(_socket.getfd(),SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n));
    if(ret)
    {
        perror("setsocketopt");
        return;
    }
}

    void Acceptor::setreuseport()
{
    int n=0;
    int ret=setsockopt(_socket.getfd(),SOL_SOCKET,SO_REUSEPORT,&n,sizeof(n));
    if(ret)
    {
        perror("setsocketopt");
        return;
    }
}

    void Acceptor::bind()
{
    int ret = ::bind(_socket.getfd(),(struct sockaddr*)_addr.getaddr(),sizeof(sockaddr));
    if(ret==-1)
    {
        perror("::bind ");
        return ;
    }
}

    void Acceptor::listen()
{
    int ret = ::listen(_socket.getfd(),10);
    if(ret ==-1)
    {
        perror(" ::listen");
        return ;
    }
}

    int Acceptor::accept()
{
    int netfd = ::accept(_socket.getfd(),nullptr,nullptr);
    if(netfd==-1)
    {
        perror("::accept");
        return -1;
    }
    return netfd;
}

    int Acceptor::fd()
{
    return _socket.getfd();
}
